package com.eyaomall.wxmall.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.lycheeframework.core.common.util.DateUtil;

public class DateUtils {
	private static final Logger LOG = LoggerFactory.getLogger(DateUtils.class);

	private static final SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd" );


	 /** 
     * 获取当年的最后一天 
     * @param year 
     * @return 
     */  
	public static Date getCurrYearLast(){  
        Calendar currCal = Calendar.getInstance();    
        int currentYear = currCal.get(Calendar.YEAR);  
        return getYearLast(currentYear);  
    }
    
    /** 
     * 获取某年最后一天日期 
     * @param year 年份 
     * @return Date 
     */  
	public static Date getYearLast(int year){  
        Calendar calendar = Calendar.getInstance();  
        calendar.clear();  
        calendar.set(Calendar.YEAR, year);  
        calendar.roll(Calendar.DAY_OF_YEAR, -1);  
        Date currYearLast = calendar.getTime();  
          
        return currYearLast;  
    } 
	
	/**
	 * 比较当前日期
	 * @param start
	 * @return
	 */
	public static boolean compare(Date start){
		
		return start.compareTo(DateUtil.parseDate(DateUtil.formatYYYYMMDD(new Date()))) < 0 ? true : false;
	}
	
	/**
	 * 获取当前时间前后几小时的时间
	 * @param hour
	 * @return
	 */
	public static Date getTimeByHour(int hour) {

        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour);

        return  calendar.getTime();
        
	}
	
	/**
	 * 字符串转日期，格式yyyy-MM-dd
	 * @param datestr
	 * @return
	 */
	public static Date dateParse(String datestr){
		
		 try {
			return sdf.parse(datestr);
		} catch (ParseException e) {
			LOG.error(e.getMessage());
			return null;
		}
	}
	
	/**
	 * 格式化时间，yyyy-mm-dd
	 * @param date
	 * @return
	 */
	public static String dateFormat(Date date){
		return sdf.format(date);
	}
	/**
	 * 获取给定时间几天后的时间
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date dateAfterDays(Date date,int day){
		long time=date.getTime()+day*24*60*60*1000;
		return new Date(time);
	}
	
	/**
	 * 获取给定时间分钟的时间
	 * @param date
	 * @param min
	 * @return
	 */
	public static Date dateAfterMin(Date date,int min){
		long time=date.getTime()+min*60*1000;
		return new Date(time);
	}
	/**
	 * 获取指定时间的cron表达式
	 * @param date
	 * @return
	 */
	public static String dateToCron(Date date){
		StringBuffer sb =new StringBuffer();
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		sb.append(now.get(Calendar.SECOND));
		sb.append(" ");
		sb.append(now.get(Calendar.MINUTE));
		sb.append(" ");
		sb.append(now.get(Calendar.HOUR_OF_DAY));
		sb.append(" ");
		sb.append(now.get(Calendar.DAY_OF_MONTH));
		sb.append(" ");
		sb.append((now.get(Calendar.MONTH) + 1));
		sb.append(" ");
		sb.append("?");
		sb.append(" ");
		sb.append(now.get(Calendar.YEAR));
		return sb.toString();
	}
	public static void main(String args[]){
//		 Calendar now = Calendar.getInstance();  
//	        System.out.println("年: " + now.get(Calendar.YEAR));  
//	        System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");  
//	        System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));  
//	        System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));  
//	        System.out.println("分: " + now.get(Calendar.MINUTE));  
//	        System.out.println("秒: " + now.get(Calendar.SECOND));  
//		 System.out.println(dateToCron(dateAfterMin(new Date(),60)));
		 System.out.println(JSON.toJSONString(new Integer(1)));
	}
}
